<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>I2C-24C02 读写实验平台</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/Chart.js/4.4.0/chart.min.js"></script>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body class="bg-gray-100">
    <div class="container mx-auto px-4 py-8">
        <h1 class="text-3xl font-bold text-center mb-8 text-blue-600">
            <i class="fas fa-microchip mr-2"></i>I2C-24C02 读写实验平台
        </h1>
        
        <!-- 配置区域 -->
        <div class="bg-white rounded-lg shadow-md p-6 mb-6">
            <h2 class="text-xl font-semibold mb-4">
                <i class="fas fa-cog mr-2"></i>配置设置
            </h2>
            <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                <div>
                    <label class="block text-sm font-medium mb-2">器件地址 (7位)</label>
                    <input type="number" id="deviceAddress" min="0" max="127" value="80" 
                           class="w-full px-3 py-2 border rounded-md">
                </div>
                <div>
                    <label class="block text-sm font-medium mb-2">写保护</label>
                    <select id="writeProtect" class="w-full px-3 py-2 border rounded-md">
                        <option value="false">关闭</option>
                        <option value="true">启用</option>
                    </select>
                </div>
                <div class="flex items-end">
                    <button onclick="updateConfig()" 
                            class="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600">
                        <i class="fas fa-save mr-2"></i>更新配置
                    </button>
                </div>
            </div>
        </div>

        <!-- 操作区域 -->
        <div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6">
            <!-- 写入区域 -->
            <div class="bg-white rounded-lg shadow-md p-6">
                <h2 class="text-xl font-semibold mb-4">
                    <i class="fas fa-edit mr-2"></i>写入操作
                </h2>
                <div class="space-y-4">
                    <div>
                        <label class="block text-sm font-medium mb-2">起始地址 (0-255)</label>
                        <input type="number" id="writeAddress" min="0" max="255" value="0" 
                               class="w-full px-3 py-2 border rounded-md">
                    </div>
                    <div>
                        <label class="block text-sm font-medium mb-2">数据 (十六进制，空格分隔)</label>
                        <textarea id="writeData" rows="3" placeholder="例如: 48 65 6C 6C 6F"
                                  class="w-full px-3 py-2 border rounded-md"></textarea>
                    </div>
                    <div class="flex space-x-2">
                        <button onclick="writeSingleByte()" 
                                class="bg-green-500 text-white px-4 py-2 rounded-md hover:bg-green-600">
                            <i class="fas fa-plus mr-2"></i>单字节写入
                        </button>
                        <button onclick="writePage()" 
                                class="bg-purple-500 text-white px-4 py-2 rounded-md hover:bg-purple-600">
                            <i class="fas fa-layer-group mr-2"></i>页写入
                        </button>
                    </div>
                </div>
            </div>

            <!-- 读取区域 -->
            <div class="bg-white rounded-lg shadow-md p-6">
                <h2 class="text-xl font-semibold mb-4">
                    <i class="fas fa-download mr-2"></i>读取操作
                </h2>
                <div class="space-y-4">
                    <div>
                        <label class="block text-sm font-medium mb-2">起始地址 (0-255)</label>
                        <input type="number" id="readAddress" min="0" max="255" value="0" 
                               class="w-full px-3 py-2 border rounded-md">
                    </div>
                    <div>
                        <label class="block text-sm font-medium mb-2">读取长度 (1-256)</label>
                        <input type="number" id="readLength" min="1" max="256" value="16" 
                               class="w-full px-3 py-2 border rounded-md">
                    </div>
                    <div class="flex space-x-2">
                        <button onclick="readRandom()" 
                                class="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600">
                            <i class="fas fa-search mr-2"></i>随机读取
                        </button>
                        <button onclick="readSequential()" 
                                class="bg-indigo-500 text-white px-4 py-2 rounded-md hover:bg-indigo-600">
                            <i class="fas fa-list mr-2"></i>顺序读取
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 数据展示区域 -->
        <div class="bg-white rounded-lg shadow-md p-6 mb-6">
            <h2 class="text-xl font-semibold mb-4">
                <i class="fas fa-database mr-2"></i>数据展示
            </h2>
            <div class="grid grid-cols-1 lg:grid-cols-3 gap-4">
                <div>
                    <h3 class="font-medium mb-2">十六进制</h3>
                    <div id="hexDisplay" class="bg-gray-100 p-3 rounded-md font-mono text-sm"></div>
                </div>
                <div>
                    <h3 class="font-medium mb-2">ASCII</h3>
                    <div id="asciiDisplay" class="bg-gray-100 p-3 rounded-md font-mono text-sm"></div>
                </div>
                <div>
                    <h3 class="font-medium mb-2">十进制</h3>
                    <div id="decimalDisplay" class="bg-gray-100 p-3 rounded-md font-mono text-sm"></div>
                </div>
            </div>
        </div>

        <!-- 波形图区域 -->
        <div class="bg-white rounded-lg shadow-md p-6 mb-6">
            <h2 class="text-xl font-semibold mb-4">
                <i class="fas fa-chart-line mr-2"></i>I2C时序波形
            </h2>
            <canvas id="waveformChart" width="800" height="200"></canvas>
        </div>

        <!-- 日志区域 -->
        <div class="bg-white rounded-lg shadow-md p-6">
            <div class="flex justify-between items-center mb-4">
                <h2 class="text-xl font-semibold">
                    <i class="fas fa-list-alt mr-2"></i>I2C操作日志
                </h2>
                <button onclick="clearLog()" 
                        class="bg-red-500 text-white px-3 py-1 rounded-md hover:bg-red-600 text-sm">
                    <i class="fas fa-trash mr-1"></i>清除日志
                </button>
            </div>
            <div id="logDisplay" class="bg-gray-100 p-4 rounded-md font-mono text-sm max-h-64 overflow-y-auto"></div>
        </div>
    </div>

    <script src="{{ url_for('static', filename='js/app.js') }}"></script>
</body>
</html>
